﻿using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using 基于WPF的电缆采样数据分析系统.Models.Task;
using 基于WPF的电缆采样数据分析系统.Models.Enums;
using System.Windows;
using 基于WPF的电缆采样数据分析系统.Service;
using Prism.Services.Dialogs;
using Prism.Ioc;
using 基于WPF的电缆采样数据分析系统.Tools.LangugeTool;
using 基于WPF的电缆采样数据分析系统.Tools.MessagePopupTool;
using 基于WPF的电缆采样数据分析系统.ViewModels.TDRWork.MenuPage;
using Prism.Events;
using 基于WPF的电缆采样数据分析系统.MsgEvents;
using 基于WPF的电缆采样数据分析系统.Tools.ProjectTools;
using 基于WPF的电缆采样数据分析系统.Views.TDRWork.TaskPage;
using static SkiaSharp.HarfBuzz.SKShaper;
using 基于WPF的电缆采样数据分析系统.Models.RefWav;
using System.Collections.ObjectModel;
using 基于WPF的电缆采样数据分析系统.Tools.TaskListTools;
using 基于WPF的电缆采样数据分析系统.Tools.LogTools;
using 基于WPF的电缆采样数据分析系统.MsgEvents.DeviceStatus;
using 基于WPF的电缆采样数据分析系统.MsgEvents.AccountingReport;
using 基于WPF的电缆采样数据分析系统.Models.Dbs;
using 基于WPF的电缆采样数据分析系统.Models.Dbs.DbService;
using 基于WPF的电缆采样数据分析系统.MsgEvents.HistoricalData;

namespace 基于WPF的电缆采样数据分析系统.ViewModels.TDRWork
{
    public class TaskPageUCViewModel : BindableBase
    {
        /// <summary>
        /// 对话服务
        /// </summary>
        private readonly DialogHostService DialogHostService;

        /// <summary>
        /// 事件聚合器（用于发布订阅）
        /// </summary>
        private readonly IEventAggregator Aggregator;


        #region 当前数据
        /// <summary>
        /// 当前任务集合
        /// </summary>
        private List<TaskDataInfo> _CurrentTaskList;

        /// <summary>
		/// 当前任务集合
		/// </summary>
        public List<TaskDataInfo> CurrentTaskList
        {
			get { return _CurrentTaskList; }
			set { 
				_CurrentTaskList = value;
				RaisePropertyChanged();
			}
		}

        /// <summary>
        /// 当前选中任务
        /// </summary>
        private TaskDataInfo _CurrentTaskDataInfo;

        /// <summary>
        /// 当前选中任务
        /// </summary>
        public TaskDataInfo CurrentTaskDataInfo
        {
            get { return _CurrentTaskDataInfo; }
            set
            {
                _CurrentTaskDataInfo = value;
                RaisePropertyChanged();
            }
        }

        /// <summary>
        /// 当前显示波形数据
        /// </summary>
        private List<float> _CurrentWaveformData;

        /// <summary>
        /// 当前显示波形数据
        /// </summary>
        public List<float> CurrentWaveformData
        {
            get { return _CurrentWaveformData; }
            set { 
                _CurrentWaveformData = value;
                RaisePropertyChanged();
            }
        }

        



        /// <summary>
        /// 刷新任务列表
        /// </summary>
        private void Refresh()
        {
            var temp = new List<TaskDataInfo>();

            for (var i = 0; i < CurrentTaskList.Count; i++)
            {
                CurrentTaskList[i].Tid = i + 1;
                temp.Add(CurrentTaskList[i]);
            }

            CurrentTaskList = temp;
        }

        /// <summary>
        /// 传值
        /// </summary>
        private TaskDataInfo PassValue()
        {
            var tempTask = new TaskDataInfo();
            tempTask.Tid = CurrentTaskList.Count + 1;
            tempTask.DescriptionInfo = CurrentTaskDataInfo.DescriptionInfo;
            tempTask.ChannelConfig = CurrentTaskDataInfo.ChannelConfig;
            tempTask.HorizontalArgument = CurrentTaskDataInfo.HorizontalArgument;
            tempTask.VerticalArgument = CurrentTaskDataInfo.VerticalArgument;
            tempTask.CreateUser = CurrentTaskDataInfo.CreateUser;
            tempTask.UpdateUser = CurrentTaskDataInfo.UpdateUser;
            tempTask.CreateTime = DateTime.Now;
            tempTask.UpdateTime = DateTime.Now;

            return tempTask;
        }
        #endregion


        /// <summary>
        /// 构造器
        /// </summary>
        public TaskPageUCViewModel(DialogHostService _DialogHostService, IEventAggregator _Aggregator)
        {
            // 对话框
            DialogHostService = _DialogHostService;

            // 发布订阅
            Aggregator = _Aggregator;
            // 通道更新
            Aggregator.GetEvent<MsgEvent>().Subscribe(SynchronizationCurrentTask);
            // 保存当前工程事件
            Aggregator.GetEvent<MsgSaveEvent>().Subscribe(SaveCurrentProject);
            // 根据文件名保存当前工程事件
            Aggregator.GetEvent<MsgByFileNameSaveEvent>().Subscribe(ByFileNameSaveCurrentProject);
            // 显示创建工程的第一个任务的波形数据
            Aggregator.GetEvent<MsgCreateProjectFirstTaskWav>().Subscribe(CreateProjectDefaultTaskRefreshWavform);
            // 保存当前参考波形
            Aggregator.GetEvent<MsgSaveRefWavData>().Subscribe(SaveCurrentRefWavform);

            // 绑定选中任务
            TaskDataBoxCmm = new DelegateCommand<int?>(TaskDataBoxFun);

            // 绑定删除任务
            DeleteTaskCmm = new DelegateCommand(DeleteTaskFun);

            // 绑定复制任务
            CopyTaskCmm = new DelegateCommand(CopyTaskFun);

            // 绑定粘贴任务
            PasteTaskCmm = new DelegateCommand(PasteTaskFun);

            // 绑定新建任务
            CreateTaskCmm = new DelegateCommand(CreateTaskFun);

            // 绑定编辑任务
            EditTaskCmm = new DelegateCommand(EditTaskFun);

            // 绑定导出任务
            ExportTaskCmm = new DelegateCommand(ExportTaskFun);

            // 绑定最近使用文件
            RecentUsedFileCmm = new DelegateCommand(RecentUsedFileFun);

            // 绑定上移和下移
            MoveCmm = new DelegateCommand<string>(MoveFun);
        }


        #region 选中任务
        /// <summary>
        /// 选中任务命令
        /// </summary>
        public DelegateCommand<int?> TaskDataBoxCmm { get; set; }

        /// <summary>
        /// 选中任务方法
        /// </summary>
        private void TaskDataBoxFun(int? selectedIndex)
		{
            CurrentTid = (int)selectedIndex + 1;

            // 获取当前选中任务的通道类型
            if (CurrentTaskList.FirstOrDefault(t => t.Tid == CurrentTid) == null)
            {
                return;
            }

            // 获取当前任务信息
            CurrentTaskDataInfo = CurrentTaskList.FirstOrDefault(t => t.Tid == CurrentTid);

            // 获取当前任务下标
            var channelConfig = CurrentTaskDataInfo.ChannelConfig;

            // 初始化下方菜单栏数据
            Aggregator.GetEvent<MsgEvent>().Publish(CurrentTaskDataInfo);

            // 如果还未导入波形数据或为生成仿真数据
            switch (channelConfig)
            {
                case PassageType.ChannelOne:
                    if (App.ChannelDataList[0].PData == null)
                    {
                        RealTimeOscilloscopeViewModel.Instance.ClearWavform();
                        return;
                    }
                    break;
                case PassageType.ChannelTwo:
                    if (App.ChannelDataList[1].PData == null)
                    {
                        RealTimeOscilloscopeViewModel.Instance.ClearWavform();
                        return;
                    }
                    break;
                case PassageType.ChannelThree:
                    if (App.ChannelDataList[2].PData == null)
                    {
                        RealTimeOscilloscopeViewModel.Instance.ClearWavform();
                        return;
                    }
                    break;
            }

            // 根据选中任务的通道显示对应通道波形
            switch (channelConfig)
            {
                case PassageType.ChannelOne:
                    CurrentWaveformData = App.ChannelDataList[0].PData;
                    break;
                case PassageType.ChannelTwo:
                    CurrentWaveformData = App.ChannelDataList[1].PData;
                    break;
                case PassageType.ChannelThree:
                    CurrentWaveformData = App.ChannelDataList[2].PData;
                    break;
            }

            // 根据通道类型和任务信息
            UpdateWavList updateWavList = ByChannelInfoUpdateWavData(channelConfig, CurrentTaskDataInfo);

            // 将当前波形文件数据赋值到全局中
            GlobalVariableEdit.CurrentWavList = updateWavList;

            // 渲染示波器
            if (CurrentWaveformData.Count > 0)
            {
                RealTimeOscilloscopeViewModel.Instance.ShowWavform(updateWavList, true);
            }
        }

        /// <summary>
        /// 当前选中任务id
        /// </summary>
        private int _CurrentTid;

        /// <summary>
        /// 当前选中任务id
        /// </summary>
        public int CurrentTid
        {
            get { return _CurrentTid; }
            set { 
                _CurrentTid = value;
                RaisePropertyChanged();
            }
        }


        /// <summary>
        /// 根据通道类型和任务信息
        /// </summary>
        /// <param name="passageType"></param>
        /// <param name="task"></param>
        /// <returns></returns>
        private UpdateWavList ByChannelInfoUpdateWavData(PassageType passageType, TaskDataInfo task)
        {
            UpdateWavList updateWavList = new UpdateWavList();

            List<float> newWavList = new List<float>();

            switch (passageType)
            {
                case PassageType.ChannelOne:
                    newWavList = App.ChannelDataList[0].PData;
                    break;
                case PassageType.ChannelTwo:
                    newWavList = App.ChannelDataList[1].PData;
                    break;
                case PassageType.ChannelThree:
                    newWavList = App.ChannelDataList[2].PData;
                    break;
            }

            // 任务对应通道无数据， 示波器清空
            if (newWavList == null)
            {
                MessagePopup.DisplayMessagePopup(LangugeConvertor.GetValue("TheChannelCorrespondingToTheTaskHasNoData"));
                RealTimeOscilloscopeViewModel.Instance.ClearWavform();
                return null;
            }

            // 刷新数据
            var refreshWavList = new List<float>();
            for (int i = 0; i < newWavList.Count; i++)
            {
                refreshWavList.Add(newWavList[i]);
            }

            updateWavList.WavList = refreshWavList;
            updateWavList.XAxisTranslation = task.XAxisTranslation;
            updateWavList.YAxisTranslation = task.YAxisTranslation;

            return updateWavList;
        }

        /// <summary>
        /// 渲染创建工程时的第一个任务的波形数据
        /// </summary>
        private void CreateProjectDefaultTaskRefreshWavform()
        {
            var taskDataInfo = CurrentTaskList[0];

            UpdateWavList updateWavList = ByChannelInfoUpdateWavData(taskDataInfo.ChannelConfig, taskDataInfo);

            // 渲染示波器
            if (updateWavList != null)
            {
                RealTimeOscilloscopeViewModel.Instance.ShowWavform(updateWavList, true);
            }
        }

        #endregion


        #region 删除任务
        /// <summary>
        /// 删除任务命令
        /// </summary>
        public DelegateCommand DeleteTaskCmm { get; set; }

        /// <summary>
        /// 删除任务方法
        /// </summary>
        public async void DeleteTaskFun()
        {
            // 未选中时点击删除
            if (CurrentTid == 0)
            {
                // 消息推送
                MessagePopup.DisplayMessagePopup(LangugeConvertor.GetValue("NoTaskIsSelectedAndCannotDeleted"));
                return;
            }

            // 点击删除前已选中
            DialogParameters paras = new DialogParameters();
            paras.Add("CurrentTid", CurrentTid);

            var result = await DialogHostService.ShowDialog("DeleteTaskUC", paras);

            if (result.Result == ButtonResult.Yes)
            {
                // 消息推送
                MessagePopup.DisplayMessagePopup(LangugeConvertor.GetValue("SuccessfullyDeleted"));

                // 删除选中任务
                CurrentTaskList.RemoveAt(CurrentTid - 1);

                // 更新操作日志
                GlobalLogTool.Instance.AddLogToTDRWork(GlobalVariableEdit.CurrentUserInfo.Uid, LangugeConvertor.GetValue("SuccessfullyDeleted") + ": 【" + LangugeConvertor.GetValue("TheTaskIdIs") + " " + CurrentTid + "】");

                // 刷新任务列表
                Refresh();

               
            }
        }
        #endregion


        #region 复制任务
        /// <summary>
        /// 复制任务命令
        /// </summary>
        public DelegateCommand CopyTaskCmm { get; set; }

        /// <summary>
        /// 复制任务方法
        /// </summary>
        public void CopyTaskFun()
        {
            // 未选中时点击复制
            if (CurrentTid == 0)
            {
                // 消息推送
                MessagePopup.DisplayMessagePopup(LangugeConvertor.GetValue("NoTaskIsSelectedAndCannotCopied"));
                return;
            }

            // 消息推送
            MessagePopup.DisplayMessagePopup(LangugeConvertor.GetValue("SuccessfullyCopied"));

            CurrentTaskDataInfo = CurrentTaskList.FirstOrDefault(t => t.Tid == CurrentTid);
        }

        #endregion


        #region 粘贴任务
        /// <summary>
        /// 粘贴任务命令
        /// </summary>
        public DelegateCommand PasteTaskCmm { get; set; }

        /// <summary>
        /// 粘贴任务方法
        /// </summary>
        public void PasteTaskFun()
        {
            // 未选中时点击粘贴
            if (CurrentTid == 0)
            {
                // 消息推送
                MessagePopup.DisplayMessagePopup(LangugeConvertor.GetValue("NotCopiedCannotPaste"));
                return;
            }

            // 找不到复制的任务
            if (CurrentTaskDataInfo == null)
            {
                // 消息推送
                MessagePopup.DisplayMessagePopup(LangugeConvertor.GetValue("NotCopiedCannotPaste"));
                return;
            }

            // 消息推送
            MessagePopup.DisplayMessagePopup(LangugeConvertor.GetValue("SuccessfullyPasted"));
            CurrentTaskList.Add(PassValue());

            // 更新操作日志
            GlobalLogTool.Instance.AddLogToTDRWork(GlobalVariableEdit.CurrentUserInfo.Uid, LangugeConvertor.GetValue("SuccessfullyPasted") + ": 【" + LangugeConvertor.GetValue("TheTaskIdIs") + " " + CurrentTaskList.Count + "】");

            // 刷新任务列表
            Refresh();
        }
        #endregion


        #region 新建任务
        /// <summary>
        /// 新建任务命令
        /// </summary>
        public DelegateCommand CreateTaskCmm { get; set; }

        /// <summary>
        /// 新建任务方法
        /// </summary>
        public async void CreateTaskFun()
        {
            if (string.IsNullOrEmpty(GlobalVariableEdit.FilePath))
            {
                // 消息提示
                MessagePopup.DisplayMessagePopup(LangugeConvertor.GetValue("UnableToCreateTask"));
                return;
            }

            DialogParameters paras = new DialogParameters();
            paras.Add("pattern", LangugeConvertor.GetValue("NewTask"));
            paras.Add("TaskContent", GlobalVariableEdit.TaskContent);
            paras.Add("CurrentChannelIndex", GlobalVariableEdit.CurrentChannelIndex);
            paras.Add("XAxisStartPoint", GlobalVariableEdit.XAxisStartPoint);
            paras.Add("XAxisEndPoint", GlobalVariableEdit.XAxisEndPoint);
            paras.Add("XAxisScale", GlobalVariableEdit.XAxisScale);
            paras.Add("YAxisStartPoint", GlobalVariableEdit.YAxisStartPoint);
            paras.Add("YAxisEndPoint", GlobalVariableEdit.YAxisEndPoint);
            paras.Add("YAxisScale", GlobalVariableEdit.YAxisScale);
            paras.Add("XAxisTranslation", GlobalVariableEdit.XAxisTranslation);
            paras.Add("YAxisTranslation", GlobalVariableEdit.YAxisTranslation);

            var result = await DialogHostService.ShowDialog("TaskPageInfoUC", paras);

            // 新建任务确定
            if (result.Result == ButtonResult.Yes)
            {
                if (result.Parameters.ContainsKey("Task"))
                {
                    // 消息提示
                    MessagePopup.DisplayMessagePopup(LangugeConvertor.GetValue("CreatingTaskSucceeded"));

                    TaskDataInfo task = result.Parameters.GetValue<TaskDataInfo>("Task");
                    task.Tid = CurrentTaskList.Count() + 1;
                    task.CreateUser = GlobalVariableEdit.CurrentUserInfo.Uid;
                    CurrentTaskList.Add(task);
                    CurrentTid = task.Tid;

                    // 更新操作日志
                    GlobalLogTool.Instance.AddLogToTDRWork(GlobalVariableEdit.CurrentUserInfo.Uid, LangugeConvertor.GetValue("CreatingTaskSucceeded") + ": 【" + LangugeConvertor.GetValue("TheTaskIdIs") + " " + CurrentTid + "】");

                    // 刷新任务列表
                    Refresh(); 
                }
            }
        }
        #endregion


        #region 编辑任务
        /// <summary>
        /// 编辑任务命令
        /// </summary>
        public DelegateCommand EditTaskCmm { get; set; }

        /// <summary>
        /// 编辑任务方法
        /// </summary>
        public async void EditTaskFun()
        {
            // 未选中时点击删除
            if (CurrentTid == 0)
            {
                // 消息推送
                MessagePopup.DisplayMessagePopup(LangugeConvertor.GetValue("UnableToDditTask"));
                return;
            }

            // 获取当前任务
            CurrentTaskDataInfo = CurrentTaskList.FirstOrDefault(t => t.Tid == CurrentTid);

            DialogParameters paras = new DialogParameters();
            paras.Add("pattern", LangugeConvertor.GetValue("RW_EditTask"));
            paras.Add("TaskContent", CurrentTaskDataInfo.DescriptionInfo);
            paras.Add("CurrentChannelIndex", CurrentTaskDataInfo.ChannelConfig);
            paras.Add("XAxisStartPoint", CurrentTaskDataInfo.HorizontalArgument.AxisOrigin);
            paras.Add("XAxisEndPoint", CurrentTaskDataInfo.HorizontalArgument.AxisEndPoint);
            paras.Add("XAxisScale", CurrentTaskDataInfo.HorizontalArgument.AxisScale);
            paras.Add("YAxisStartPoint", CurrentTaskDataInfo.VerticalArgument.AxisOrigin);
            paras.Add("YAxisEndPoint", CurrentTaskDataInfo.VerticalArgument.AxisEndPoint);
            paras.Add("YAxisScale", CurrentTaskDataInfo.VerticalArgument.AxisScale);
            paras.Add("XAxisTranslation", CurrentTaskDataInfo.XAxisTranslation);
            paras.Add("YAxisTranslation", CurrentTaskDataInfo.YAxisTranslation);

            var result = await DialogHostService.ShowDialog("TaskPageInfoUC", paras);

            // 修改任务确定
            if (result.Result == ButtonResult.Yes)
            {
                if (result.Parameters.ContainsKey("Task"))
                {
                    var updateTaskId = CurrentTid;

                    // 消息提示
                    MessagePopup.DisplayMessagePopup(LangugeConvertor.GetValue("EditTaskSuccessful"));

                    TaskDataInfo task = result.Parameters.GetValue<TaskDataInfo>("Task");
                    task.Tid = CurrentTid;
                    task.CreateTime = CurrentTaskDataInfo.CreateTime;
                    task.CreateUser = CurrentTaskDataInfo.CreateUser;
                    CurrentTaskList[CurrentTid - 1] = task;
                    CurrentTaskDataInfo = task;

                    // 更新操作日志
                    GlobalLogTool.Instance.AddLogToTDRWork(GlobalVariableEdit.CurrentUserInfo.Uid, LangugeConvertor.GetValue("EditTaskSuccessful") + ": 【" + LangugeConvertor.GetValue("TheTaskIdIs") + " " + CurrentTid + "】");

                    // 刷新任务列表
                    Refresh();

                    // 刷新菜单-通道内容
                    Aggregator.GetEvent<MsgEvent>().Publish(CurrentTaskList[updateTaskId - 1]);

                    // 根据通道类型和任务信息
                    UpdateWavList updateWavList = ByChannelInfoUpdateWavData(task.ChannelConfig, CurrentTaskDataInfo);

                    // 将当前波形文件数据赋值到全局中
                    GlobalVariableEdit.CurrentWavList = updateWavList;

                    // 渲染示波器
                    if (CurrentWaveformData == null)
                    {
                        return;
                    }

                    if (CurrentWaveformData.Count > 0)
                    {
                        RealTimeOscilloscopeViewModel.Instance.ShowWavform(updateWavList, true);
                    }
                }
            }
        }
        #endregion


        #region 导出任务
        /// <summary>
        /// 导出任务命令
        /// </summary>
        public DelegateCommand ExportTaskCmm { get; set; }

        /// <summary>
        /// 导出任务方法
        /// </summary>
        public async void ExportTaskFun()
        {
            if (CurrentTaskList == null || CurrentTaskList.Count == 0)
            {
                MessagePopup.DisplayMessagePopup(LangugeConvertor.GetValue("ExportTaskFailed"));
                return;
            }

            DialogParameters paras = new DialogParameters();
            paras.Add("CurrentTaskList", CurrentTaskList);
            var result = await DialogHostService.ShowDialog("ImportAndExportUC", paras);

            if (result.Result == ButtonResult.Yes)
            {
                if (result.Parameters.ContainsKey("IndexList"))
                {
                    List<int> list = result.Parameters.GetValue<List<int>>("IndexList");

                    ObservableCollection<TaskDataInfo> currentTaskList = new ObservableCollection<TaskDataInfo>();

                    for (int i = 0; i < list.Count; i++)
                    {
                        currentTaskList.Add(CurrentTaskList.FirstOrDefault(t => t.Tid == list[i]));
                    }

                    RetResult retResult = ExportTaskList.ByFilePathExportTaskList(currentTaskList);

                    if (retResult == RetResult.Success)
                    {
                        MessagePopup.DisplayMessagePopup(LangugeConvertor.GetValue("SucceededInExportingTheTaskCollectionProcedure"));

                        // 更新设备状态界面的文件使用
                        Aggregator.GetEvent<MsgFileUsedStatus>().Publish();

                        // 更新使用文件记录
                        Aggregator.GetEvent<MsgUpdateShowUsedFileLogList>().Publish();

                        // 添加任务导出信息
                        GetTaskDataInfoDAOList(currentTaskList);

                        // 更新历史上数据区
                        Aggregator.GetEvent<MsgHistoricalExportTaskRecords>().Publish();
                    } else
                    {
                        MessagePopup.DisplayMessagePopup(LangugeConvertor.GetValue("TheExportTaskCollectionHasBeenCancelledProcedure"));
                    }
                }
            }
        }


        /// <summary>
        /// 添加任务导出信息
        /// </summary>
        /// <param name="currentTaskList"></param>
        private void GetTaskDataInfoDAOList(ObservableCollection<TaskDataInfo> currentTaskList)
        {
            ObservableCollection<TaskDataInfoDAO> taskDataInfoDAOs = new ObservableCollection<TaskDataInfoDAO>();

            for (int i = 0; i < currentTaskList.Count; i++)
            {
                taskDataInfoDAOs.Add(new TaskDataInfoDAO()
                {
                    DescriptionInfo = currentTaskList[i].DescriptionInfo,
                    ChannelConfig = currentTaskList[i].ChannelConfig,
                    CreateUser = currentTaskList[i].CreateUser,
                    UpdateUser = currentTaskList[i].UpdateUser,
                    CreateTime = currentTaskList[i].CreateTime,
                    UpdateTime = currentTaskList[i].UpdateTime,
                    XAxisTranslation = currentTaskList[i].XAxisTranslation,
                    YAxisTranslation = currentTaskList[i].YAxisTranslation,
                });
            }

            for (int i = 0; i < taskDataInfoDAOs.Count; i++)
            {
                TaskDataInfoDAOService.Instance.AddTaskDataInfoDAO(taskDataInfoDAOs[i]);
            }
        }



        #endregion


        #region 最近使用文件
        /// <summary>
        /// 最近使用文件命令
        /// </summary>
        public DelegateCommand RecentUsedFileCmm { get; set; }

        /// <summary>
        /// 最近使用文件方法
        /// </summary>
        public async void RecentUsedFileFun()
        {
            var result = await DialogHostService.ShowDialog("RecentUsedFileUC", null);
        }
        #endregion


        #region 上下移动之后,更新通道信息
        public DelegateCommand<string> MoveCmm { get; set; }

        private void MoveFun(string str)
        {
            if (string.Equals(str, "UP"))
            {

                if (CurrentTaskList == null || CurrentTaskList.Count == 0)
                {
                    MessagePopup.DisplayMessagePopup(LangugeConvertor.GetValue("FailedToMoveUpTask"));
                    return;
                }

                if (CurrentTid > 1)
                {

                    CurrentTid -= 1;
                    // 获取当前任务信息
                    CurrentTaskDataInfo = CurrentTaskList.FirstOrDefault(t => t.Tid == CurrentTid);
                    // 更新菜单栏数据
                    Aggregator.GetEvent<MsgEvent>().Publish(CurrentTaskDataInfo);

                    // 更新的波形数据
                    UpdateWavList updateWavList = ByChannelInfoUpdateWavData(CurrentTaskDataInfo.ChannelConfig, CurrentTaskDataInfo);

                    // 将当前波形文件数据赋值到全局中
                    GlobalVariableEdit.CurrentWavList = updateWavList;

                    // 渲染示波器
                    if (updateWavList != null)
                    {
                        RealTimeOscilloscopeViewModel.Instance.ShowWavform(updateWavList, true);
                    }
                }

                return;
            }

            if (CurrentTaskList == null || CurrentTaskList.Count == 0)
            {
                MessagePopup.DisplayMessagePopup(LangugeConvertor.GetValue("DescriptionFailedToMoveTheTaskDown"));
                return;
            }

            if (CurrentTid < CurrentTaskList.Count)
            {

                CurrentTid += 1;
                // 获取当前任务信息
                CurrentTaskDataInfo = CurrentTaskList.FirstOrDefault(t => t.Tid == CurrentTid);
                // 更新菜单栏数据
                Aggregator.GetEvent<MsgEvent>().Publish(CurrentTaskDataInfo);

                // 更新的波形数据
                UpdateWavList updateWavList = ByChannelInfoUpdateWavData(CurrentTaskDataInfo.ChannelConfig, CurrentTaskDataInfo);

                // 渲染示波器
                if (updateWavList != null)
                {
                    RealTimeOscilloscopeViewModel.Instance.ShowWavform(updateWavList, true);
                }
            }
        }

        #endregion


        #region 当前任务同步通道信息
        /// <summary>
        /// 根据通道同步当前任务
        /// </summary>
        /// <param name="taskDataInfo"></param>
        public void SynchronizationCurrentTask(TaskDataInfo taskDataInfo)
        {
            // 未选中时修改通道信息
            if (CurrentTid == 0)
            {
                // 消息推送
                MessagePopup.DisplayMessagePopup(LangugeConvertor.GetValue("TheChannelInformationCannotBeModified"));
                return;
            }


            CurrentTaskDataInfo.ChannelConfig = taskDataInfo.ChannelConfig;
            CurrentTaskDataInfo.HorizontalArgument.AxisOrigin = taskDataInfo.HorizontalArgument.AxisOrigin;
            CurrentTaskDataInfo.HorizontalArgument.AxisEndPoint = taskDataInfo.HorizontalArgument.AxisEndPoint;
            CurrentTaskDataInfo.HorizontalArgument.AxisScale = taskDataInfo.HorizontalArgument.AxisScale;
            CurrentTaskDataInfo.VerticalArgument.AxisOrigin = taskDataInfo.VerticalArgument.AxisOrigin;
            CurrentTaskDataInfo.VerticalArgument.AxisEndPoint = taskDataInfo.VerticalArgument.AxisEndPoint;
            CurrentTaskDataInfo.VerticalArgument.AxisScale = taskDataInfo.VerticalArgument.AxisScale;
            CurrentTaskDataInfo.XAxisTranslation = taskDataInfo.XAxisTranslation;
            CurrentTaskDataInfo.YAxisTranslation = taskDataInfo.YAxisTranslation;


            UpdateWavList updateWavList = ByChannelInfoUpdateWavData(taskDataInfo.ChannelConfig, taskDataInfo);

            // 将当前波形文件数据赋值到全局中
            GlobalVariableEdit.CurrentWavList = updateWavList;

            // 渲染示波器
            if (updateWavList != null)
            {
                RealTimeOscilloscopeViewModel.Instance.ShowWavform(updateWavList, false);
            }

            // 刷新任务列表
            Refresh();
        }


        #endregion


        #region 保存当前工程
        /// <summary>
        /// 保存当前工程
        /// </summary>
        public void SaveCurrentProject()
        {
            string filePath = GlobalVariableEdit.FilePath;

            GlobalVariableEdit.CurrentProjectDataInfo.TaskList = CurrentTaskList;
            GlobalVariableEdit.CurrentProjectDataInfo.UpdateTime = DateTime.Now;

            OpenProject.DirectSaveListFile(GlobalVariableEdit.CurrentProjectDataInfo, GlobalVariableEdit.FilePath);

            MessagePopup.DisplayMessagePopup(LangugeConvertor.GetValue("SaveProjectSuccess"));

            // 赋值日志文件的文件名
            string[] newStr = GlobalVariableEdit.FilePath.Split("\\");

            // 添加使用过文件的日志到集合中
            FileTypeLog.AddUsedFileLogToList(FileType.ProjectFile, newStr[newStr.Length - 1], LangugeConvertor.GetValue("SaveProjectFile"), GlobalVariableEdit.CurrentUserInfo.Uid);

            // 更新操作日志
            GlobalLogTool.Instance.AddLogToTDRWork(GlobalVariableEdit.CurrentUserInfo.Uid, LangugeConvertor.GetValue("SaveProjectFile") + ": 【" + newStr[newStr.Length - 1] + "】");
        }

        /// <summary>
        /// 根据文件名保存当前工程
        /// </summary>
        public void ByFileNameSaveCurrentProject()
        {
            GlobalVariableEdit.CurrentProjectDataInfo.TaskList = CurrentTaskList;
            GlobalVariableEdit.CurrentProjectDataInfo.UpdateTime = DateTime.Now;

            RetResult retResult = OpenProject.ByFileNameSaveListFile(GlobalVariableEdit.CurrentProjectDataInfo);

            if (retResult == RetResult.Success)
            {
                MessagePopup.DisplayMessagePopup(LangugeConvertor.GetValue("SaveProjectSuccess"));
                // 赋值日志文件的文件名
                string[] newStr = GlobalVariableEdit.FilePath.Split("\\");

                // 添加使用过文件的日志到集合中
                FileTypeLog.AddUsedFileLogToList(FileType.ProjectFile, newStr[newStr.Length - 1], LangugeConvertor.GetValue("SaveProjectFile"), GlobalVariableEdit.CurrentUserInfo.Uid);

                // 更新操作日志
                GlobalLogTool.Instance.AddLogToTDRWork(GlobalVariableEdit.CurrentUserInfo.Uid, LangugeConvertor.GetValue("SaveProjectFile") + ": 【" + newStr[newStr.Length - 1] + "】");
            }

            if (retResult == RetResult.Fail)
            {
                MessagePopup.DisplayMessagePopup(LangugeConvertor.GetValue("NoProjectSaveIsSelected"));

            }
        }

        #endregion


        #region 保存当前参考波形
        /// <summary>
        /// 保存当前参考波形
        /// </summary>
        private void SaveCurrentRefWavform()
        {

            switch (GlobalVariableEdit.CurrentRefWav)
            {
                case RefWavformType.RefWavformOne:
                    GlobalVariableEdit.R1WavList = GlobalVariableEdit.CurrentWavList;
                    break;
                case RefWavformType.RefWavformTwo:
                    GlobalVariableEdit.R2WavList = GlobalVariableEdit.CurrentWavList;
                    break;
                case RefWavformType.RefWavformThree:
                    GlobalVariableEdit.R3WavList = GlobalVariableEdit.CurrentWavList;
                    break;
                case RefWavformType.RefWavformFour:
                    GlobalVariableEdit.R4WavList = GlobalVariableEdit.CurrentWavList;
                    break;
                case RefWavformType.RefWavformFive:
                    GlobalVariableEdit.R5WavList = GlobalVariableEdit.CurrentWavList;
                    break;
                case RefWavformType.RefWavformSix:
                    GlobalVariableEdit.R6WavList = GlobalVariableEdit.CurrentWavList;
                    break;
                case RefWavformType.RefWavformSeven:
                    GlobalVariableEdit.R7WavList = GlobalVariableEdit.CurrentWavList;
                    break;
                case RefWavformType.RefWavformEight:
                    GlobalVariableEdit.R8WavList = GlobalVariableEdit.CurrentWavList;
                    break;
            }
        }

        #endregion
    }
}
